<template>
  <div class="specs">
    <el-bread></el-bread>

    <el-button @click="showAdd" class="el-btn" type="primary" size="small" plain
      >添加</el-button
    >

    <el-table
      :data="specsList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="规格编号" width="180">
      </el-table-column>
      <el-table-column prop="specsname" label="规格名称" width="180">
      </el-table-column>
      <el-table-column label="规格属性" width="250">
        <template v-slot="a">
          <el-button
            size="mini"
            type="plain"
            v-for="(item, index) in a.row.attrs"
            :key="index"
            >{{ item }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template v-slot="a">
          <el-tag v-if="a.row.status == 1" type="success">启用</el-tag>
          <el-tag v-else type="warning">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="a">
          <el-button size="small" type="primary" @click="infoShow(a.row.id)"
            >编辑</el-button
          >
          <el-button size="small" type="danger" @click="del(a.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @current-change="changePage"
      background
      layout="prev, pager, next"
      :total="specsCount"
      :page-size="pageData.size"
      :current-page="pageData.page"
    >
    </el-pagination>

    <v-dialog ref="dialog" :pageData="pageData" />
  </div>
</template>

<script>
import vDialog from "./specs-diglog.vue";
import { mapState, mapActions } from "vuex";
import { specsdelete } from "../../request/api";
export default {
  components: {
    vDialog,
  },
  data() {
    return {};
  },
  mounted() {
    // this.getSpecsList();
    this.getSpecsCount();
  },
  computed: {
    ...mapState("specs", ["specsList", "specsCount", "pageData"]),
  },
  methods: {
    ...mapActions("specs", ["getSpecsList", "getSpecsCount"]),
    showAdd() {
      this.$refs.dialog.isshow = true;
    },
    del(id) {
      this.$del(() => {
        specsdelete({ id }).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.msg);
            // this.getSpecsList();
            this.getSpecsCount();
            return;
          }
          this.$message.error(res.msg);
        });
      });
    },
    infoShow(id) {
      this.$refs.dialog.getInfo(id);
    },
    changePage(e) {
      this.$store.commit("specs/changePageData", e);
      this.getSpecsList();
    },
  },
};
</script>

<style scoped>
.el-btn {
  display: block;
  margin: 20px 0 5px 0;
}
</style>